/**
 * 业务中所用的城市-适用于火车票/机票/酒店
 * @author hongwei 2018-09-08
 */
(function (window) {
    'use strict';
    var city = function(params) {
        $.extend(this.params, params);
        this._init();   //初始化操作
    };
    city.prototype = {
        params: {
            cityOn          : ''     //获取城市标识
            ,firstStorage   : ''     //第一个storage(必传)，火车：train_start_site
            ,secondStorage  : ''     //第二个storage（必传），火车：train_end_site
            ,jsonFilePath   : ''     //json所在的地址(必传)
            ,all_city_list  : []     //所有城市列表
        }

        ,_init :function () {
            var that = this;

            //that.removeStorage();       //step1:初始删除storage
            that.getCityByJson();       //step2:通过JSON获取城市
            that.hotCityClick();        //step3:点击热门城市
            that.searchBtn();           //step4:搜索城市
            that.searchInputCity();     //step5:输入框输入城市定击确定
            that.hasSelected();         //step6:单城市列表中城市点击操作
            that.getAllCityList();      //step7:城市列表筛选
            //that.allCities();         //工具1：所有城市存储到storage
        }

        //step1:初始删除storage
        ,removeStorage: function () {
            var that = this;
            $api.rmStorage('all_cities'); //去掉所有城市的storage
            $api.rmStorage(that.params.firstStorage);
            $api.rmStorage(that.params.secondStorage);
        }

        //step2:通过JSON获取城市
        ,getCityByJson : function() {
            var that = this;
            var city_list = [];
            $.getJSON(that.params.jsonFilePath, function(ret){
                if (ret.code === 1) {
                    //step1:获取数据
                    var dataInter = ret.data;
                    city_list = dataInter;
                    $.extend(that.params.all_city_list, city_list);   //整合参数
                    //step2:模板赋值
                    var interText = doT.template($("#addressListScript").text());
                    $("#addressList").append(interText(dataInter));
                    //step3:排序
                    sort();
                    //step4:获取所有城市
                    that.allCities();
                } else {
                    alert(ret.msg);
                }
            });
        }

        //step3:点击热门城市
        ,hotCityClick: function (){
            var that = this;
            $('.hot_city').click(function () {
                var city_name = $(this).text();
                $('.search_city').val(' ');
                $('.search_city').val(city_name);
                if (parseInt(that.params.cityOn) === 0) {
                    $api.setStorage(that.params.firstStorage, city_name);
                } else {
                    $api.setStorage(that.params.secondStorage, city_name);
                }
            });
        }

        //step4:搜索城市
        ,searchBtn : function (){
            var that = this;
            $('#search').click(function () {
                that.getAllCityList($(this).value);
            });
        }

        //step5:输入框输入城市定击确定
        ,searchInputCity : function () {
            var that = this;
            $('#inputCity').on('input', function () {
                var cityName = $(this).val();
                if (parseInt(that.params.cityOn) === 0) {
                    $api.setStorage(that.params.firstStorage, cityName);
                } else {
                    $api.setStorage(that.params.secondStorage, cityName);
                }
            });
        }

        //step6:单城市列表中城市点击操作
        ,hasSelected :function (){
            var that = this;
            $('body').on('click', '.sort_list', function () {
                var cityName = $(this).children('.num_name').text();
                //留空及赋值
                $('.search_city').empty().val(cityName);
                //加对号
                $(this).siblings().children(".aui-icon-correct").addClass("hidden").removeClass("selected");
                $(this).find(".aui-icon-correct").removeClass("hidden").addClass("selected");
                //不同情况存储变量
                if (parseInt(that.params.cityOn) === 0) {
                    $api.setStorage(that.params.firstStorage, cityName);
                } else {
                    $api.setStorage(that.params.secondStorage, cityName);
                }
            })
        }

        //step7:城市列表筛选
        ,getAllCityList : function(search){
            var that = this;

            $('#addressList').empty();
            var new_list = [];
            if (search === '') {
                $.each(that.params.all_city_list, function (value, index) {
                    if (parseInt(value.name.indexOf(search)) !== -1) {
                        new_list.push(value);
                    }
                });
            } else {
                new_list = that.params.all_city_list;
            }
            if (parseInt(that.params.cityOn) === 0) {
                $api.setStorage(that.params.firstStorage, search);
            } else {
                $api.setStorage(that.params.secondStorage, search);
            }
            var interText = doT.template($("#addressListScript").text());
            $("#addressList").append(interText(new_list));
            sort();
        }

        //工具1：所有城市存储到storage
        , allCities : function () {
            var that = this;

            //所有城市
            var allCityArray = that.params.all_city_list; //所有城市
            var allCityString = [] ;
            for(var i=0; i < allCityArray.length; i++){
                allCityString.push(allCityArray[i]["name"]);
            }
            $api.setStorage('all_cities', allCityString);
        }
    };
    window.city =  city;
})(window);